package com.examples.serialization;

import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Employee implements Serializable {
	int id;
	String name;

	public void setname(String name) {
		this.name = name;
	}

}

public class SerNDeSER {

	public static void main(String[] s) {
		Employee E1 = new Employee();
		E1.id = 8;
		E1.setname("Amit");

		Employee E2 = new Employee();
		E2.id = 9;
		E2.setname("rohan");

		Employee EA[] = new Employee[10];
		for (int j = 0; j < EA.length; j++) {
			EA[j] = new Employee();
		}
		for (int i = 0; i < EA.length; i++) {
			EA[i].id = i;
			EA[i].setname("Amit" + i);
		}
		try {
			FileOutputStream fileout = new FileOutputStream("employee.ser");
			ObjectOutputStream out = new ObjectOutputStream(fileout);

			out.writeObject(E1);

			out.writeObject(E2);

			for (int i = 0; i < EA.length; i++) {
				out.writeObject(EA[i]);
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			Employee E3 = new Employee();
			FileInputStream filein = new FileInputStream("employee.ser");
			ObjectInputStream in = new ObjectInputStream(filein);

			E3 = (Employee) in.readObject();
			System.out.println(E3.id);
			System.out.println(E3.name);

			E3 = (Employee) in.readObject();
			System.out.println(E3.id);
			System.out.println(E3.name);
			System.out.println();
			System.out.println();
			for (int i = 0; i < 10; i++) {
				Employee EAA = (Employee) in.readObject();
				System.out.println(EAA.id);
				System.out.println(EAA.name);

			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
